package random.fox.p2p.presenter;

import android.support.v7.widget.RecyclerView;

import java.util.List;

import random.fox.p2p.bean.greendao.Message;
import random.fox.p2p.interactors.MessageInteractor;
import random.fox.p2p.interactors.OnLoadMessageListener;
import random.fox.p2p.ui.adapter.LikeRecycleAdapter;
import random.fox.p2p.view.ILikeView;

/**
 * Created by delong on 2015/4/1.
 */
public class LikePresenterImpl implements LikePresenter ,OnLoadMessageListener {
    private ILikeView iLikeView;
    private MessageInteractor interactor;
    private RecyclerView recyclerView;
    public LikePresenterImpl(ILikeView iLikeView,MessageInteractor interactor){
        this.iLikeView = iLikeView;
        this.interactor = interactor;
    }

    @Override
    public void loadData(RecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        List<Message> messages = interactor.getLikeCacheData();
        if(messages!=null && !messages.isEmpty()){
            onSuccess(messages);
        }else{
            iLikeView.showProgress();
            interactor.loadLikeMessage(this);
        }
    }

    @Override
    public void releaseData() {
        interactor.releaseLikeData();
    }

    @Override
    public void onSuccess(List<Message> messages) {
        iLikeView.hideProgress();
        if(recyclerView !=null){
            LikeRecycleAdapter adapter = new LikeRecycleAdapter(messages);
            recyclerView.setAdapter(adapter);
        }
    }

    @Override
    public void onError(Throwable e) {
        iLikeView.hideProgress();
    }
}
